﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using Business;
using Geekees.Common.Controls;

public partial class Controls_Common_FileManager : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetDirectory();
    }


    private void GetDirectory()
    {
        astvMyTree.RootNode.Clear();
        var path = Config.ImagePath;
        var direc = Directory.GetDirectories(path);
        var listFolder = direc.Select(s => s.Substring(s.LastIndexOf(@"\")).Replace(@"\", string.Empty)).ToList();
        listFolder.Sort((f1, f2) => f2.CompareTo(f1));
        foreach (var s in listFolder)
        {
            astvMyTree.RootNode.NodeText = "File Manager";
            astvMyTree.RootNode.NodeValue = "0";
            var filepath = path + s + "\\";
            var nodeFolder = new ASTreeViewNode(s, s) { OpenState = ASTreeViewNodeOpenState.Close };
            var files = Directory.GetFiles(filepath);
            var fileType = Request["ty"];
            var listFileType = string.Empty;
            if (fileType.ToLower() == "img")
                listFileType = ".gif .jpg .jpeg .bmp .psd .tiff .tif .png";
            if (fileType.ToLower() == "flash")
                listFileType = ".swf .flv .fla .fwt .fwt";
            if (fileType.ToLower() == "media")
                listFileType = ".avi .mpg .dat .vob .3gp .wmv .asf .mp4 .mov .3gp .mp3 .mp2 .mp1 .wav .wma .cda .aif .aiff .au .snd .flv ";
            foreach (var file in files)
            {
                var filename = file.Substring(file.LastIndexOf(@"\")).Replace(@"\", string.Empty);
                var exten = filename.Split('.');
                if (exten.Length <= 1) continue;
                if (listFileType.ToLower().IndexOf(exten[exten.Length - 1].ToLower()) < 0) continue;
                var image = Image.FromFile(file);
                var sz = image.PhysicalDimension;
                var childNode = new ASTreeViewNode(filename, filename);
                childNode.AdditionalAttributes.Add(new KeyValuePair<string, string>("onclick", "SelectNode('" +Config.ImageUrl+s+"/" + filename + "','" + sz.Width + "','" + sz.Height + "');return false;"));
                nodeFolder.AppendChild(childNode);
            }
            astvMyTree.RootNode.AppendChild(nodeFolder);
            // ObClassicTree.Nodes.Add(node);


        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
        {
            var path = Config.ImagePath + DateTime.Now.ToString("yyyyMM");
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            var fn = Path.GetFileName(FileUpload1.PostedFile.FileName);
            var saveLocation = path + "\\" + fn;
            try
            {
                FileUpload1.PostedFile.SaveAs(saveLocation);
                GetDirectory();
                Config.Message(Page, Resources.Resource.UploadSuccess);
            }
            catch (Exception ex)
            {
                Config.Message(Page, Resources.Resource.Systemerror);
                //Note: Exception.Message returns a detailed message that describes the current exception. 
                //For security reasons, we do not recommend that you return Exception.Message to end users in 
                //production environments. It would be better to put a generic error message. 
            }
        }
        else
        {
            Config.Message(Page, Resources.Resource.SelectFile);
        }

    }
}



